package com.hery.core.protocol;

import java.util.concurrent.atomic.AtomicLong;

public class Pack {
	private static final AtomicLong INVOKE_ID = new AtomicLong(0);
	private String type;
	private long id;
	private Object data;

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = newId();
	}

	public Object getData() {
		return data;
	}

	public void setData(Object data) {
		this.data = data;
	}

	public boolean isHeartbeatRequest() {
		return EventType.HEART_BEAT_REQUEST.equals(type);
	}
	public boolean isHeartbeatResponse() {
		return EventType.HEART_BEAT_RESPONSE.equals(type);
	}
	public void setHeartbeatRequest(boolean isHeartbeat) {
		if (isHeartbeat) {
			setType(EventType.HEART_BEAT_REQUEST);
		}
	}

	public void setHeartbeatResponse(boolean isHeartbeat) {
		if (isHeartbeat) {
			setType(EventType.HEART_BEAT_RESPONSE);
		}
	}

	private static long newId() {
		return INVOKE_ID.getAndIncrement();
	}

	public Pack(long id) {
		super();
		this.id = id;
	}

	public Pack() {
		this.id = INVOKE_ID.longValue();
	}
	public String toString() {
        return "Pack [id=" + id  + ", type=" + type
               + ", data=" + data==null?"":data.toString() + "]";
    }
}
